//
//  ChinaDCollectHandle.m
//  GoodBuy
//
//  Created by dlios on 15-5-26.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "ChinaDCollectHandle.h"

@implementation ChinaDCollectHandle

+ (ChinaDCollectHandle *)shareChinaHandle
{
    static ChinaDCollectHandle *dbManager = nil;
    if (dbManager == nil) {
        dbManager = [[ChinaDCollectHandle alloc] init];
    }
    return dbManager;
}
// 打开数据库
- (void)openDB
{
    // 数据库一般存放在document 文件夹下
    // 获取document 文件路径
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@", Path);
    // 拼接数据库路径
    NSString *dbPath = [Path stringByAppendingPathComponent:@"China.sqlite"];
    // 创建数据库
    // 检测路径下 是否已经存在数据库 如果不存在 才创建(sqlite3指针是否 指向了数据库)
    //  参数一: 本地的数据库路径需要UTF-8转码
    //  参数二: 数据库指针地址
    
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    if (result == SQLITE_OK) {
        NSLog( @"数据库打开成功");
        
    }else{
        NSLog(@"打开失败");
    }

}
- (void)createTable
{
    // 创建sql语句
    
    NSString *createSQL = @"create table if not exists chinas (article_id text, article_pic text, article_date text, article_price text, article_title text, article_mall text, num integer primary key autoincrement)";
    // 2.执行语句
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, nil);
    // 3.判断
    if (SQLITE_OK == result) {
        NSLog(@"创建成功");
    }else{
        NSLog(@"创建失败");
    }
}
// 插入
- (void)insertChina:(ModelDetailChina *)china
{
    NSString *insertStr = [NSString stringWithFormat:@"insert into chinas (article_id, article_pic, article_date, article_price, article_title, article_mall) values ('%@','%@','%@','%@','%@','%@')", china.article_id, china.article_pic, china.article_date,china.article_price,china.article_title, china.article_mall];
    
    // 2. 执行语句
    int result = sqlite3_exec(dbPoint, insertStr.UTF8String, NULL, NULL, nil);
    // 3. 判断
    if (SQLITE_OK == result) {
        NSLog(@"插入成功");
        
    }else{
        NSLog(@"插入失败");
    }
}
- (NSMutableArray *)selectChina
{
    // 查询操作
    // 1.从本地数据库所有信息 遍历获取每条信息
    // 2.把每条信息转化为model 对象
    // 3.把model添加在可变数组中 返回
    
    // 1.创建sql语句
    // 查询所有
    NSString *selectSQL = @"select * from chinas";
    // 2.创建替身
    sqlite3_stmt *stmt = nil;
    // 3.准备sql
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    // 4.创建存放返回数组信息
    NSMutableArray *arr = [NSMutableArray array];
    // 5.判断查询
    if (SQLITE_OK == result) {
        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 当数据库替身 指向的数据符合查询条件 在while中返回
            // 逐行 获取每一列的数据
            // 列数从0开始
            
            const unsigned char *article_id = sqlite3_column_text(stmt, 0);
            const unsigned char *article_pic = sqlite3_column_text(stmt, 1);
            const unsigned char *article_date = sqlite3_column_text(stmt, 2);
            const unsigned char *article_price = sqlite3_column_text(stmt, 3);
            const unsigned char *article_title = sqlite3_column_text(stmt, 4);
            const unsigned char *article_mall = sqlite3_column_text(stmt, 5);
            int num = sqlite3_column_int(stmt, 6);
            ModelDetailChina *temp = [[ModelDetailChina alloc] init];
            temp.article_id = [NSString stringWithUTF8String:(const char *) article_id];
            temp.article_pic = [NSString stringWithUTF8String:(const char *) article_pic];
            temp.article_date = [NSString stringWithUTF8String:(const char *) article_date];
            temp.article_price =[NSString stringWithUTF8String:(const char *) article_price];
            temp.article_title =[NSString stringWithUTF8String:(const char *) article_title];
            temp.article_mall = [NSString stringWithUTF8String:(const char *) article_mall];
            [arr addObject:temp];
        }
    }else{
        NSLog(@"查询准备失败");
    }
    return arr;
}
- (void)dropTable
{
    NSString *dropSQL = @"drop table chinas";
    int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除表成功");
    }else{
        NSLog(@"删除表 失败");
    }

}
@end
